<?
	//init configuration
  echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
  echo "<settings>\n";
  /*************************/
  /***  PHONE SETTINGS  ****/
  /*************************/
	echo "<phone-settings>\n";
	echo "<user_active idx=\"1\" perm=\"!\">on</user_active>\n";
  //client settings
  $qry="SELECT pc.*, c.`extension`, c.`password` ";
  $qry.="FROM cw_prov_client AS pc ";
  $qry.="JOIN cw_client AS c ON pc.`id_client`=c.`id` ";
  $qry.="WHERE mac='".$mac."' ";
  $res=$db->Execute($qry);
  if($res===false) printDebug("ERROR (unable to retrieve client settings)",true);
  $client=$res->FetchRow();
  echo "<user_name idx=\"1\" perm=\"&\">".sanitizeXml($client["extension"])."</user_name>\n";
  echo "<user_pass idx=\"1\" perm=\"&\">".sanitizeXml($client["password"])."</user_pass>\n";
  //template network settings
  $qry="SELECT * FROM cw_prov_template WHERE id='".$client["id_template"]."' ";
  $res=$db->Execute($qry);
  if(($res===false)||($client["id_template"]=="")) printDebug("ERROR (unable to retrieve template settings for template ".$client["id_template"].")",true);
  $template=$res->FetchRow();
  echo "<user_host idx=\"1\" perm=\"&\">".sanitizeXml($template["registrar"])."</user_host>\n";
  if($template["dhcp"]==0){ //static ip configuration
  	$ip=trim($client["ip"]);
		echo "<dhcp perm=\"&\">off</dhcp>\n";
 		echo "<ip_adr perm=\"&\">".sanitizeXml(trim($client["ip"]))."</ip_adr>\n";
    if(trim($client["hostname"])!="") echo "<phone_name perm=\"&\">".sanitizeXml(trim($client["hostname"]))."</phone_name>\n"; 
		echo "<netmask perm=\"&\">".sanitizeXml(trim($template["netmask"]))."</netmask>\n";
    echo "<gateway perm=\"&\">".sanitizeXml(trim($template["gateway"]))."</gateway>\n";
    echo "<dns_server1 perm=\"&\">".sanitizeXml(trim($template["dns1"]))."</dns_server1>\n";
    if(trim($template["domain"])!="") echo "<dns_domain perm=\"&\">".sanitizeXml(trim($template["domain"]))."</dns_domain>\n";
    if(trim($template["dns2"])!="") echo "<dns_server2 perm=\"&\">".sanitizeXml(trim($template["dns2"]))."</dns_server2>\n"; 
    //echo "<setting_server perm=\"$\">".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/index.php?mac=".$mac)."</setting_server>";
  }
  //template HTTP authentication settings
  echo "<http_user perm=\"&\">".sanitizeXml(trim($template["http_username"]))."</http_user>\n";
  echo "<http_pass perm=\"&\">".sanitizeXml(trim($template["http_password"]))."</http_pass>\n";
  if(($template["http_scheme"]==1)||(trim($template["http_password"])=="1")){
    echo "<http_scheme perm=\"&\">on</http_scheme>\n";
  }else{
    echo "<http_scheme perm=\"&\">off</http_scheme>\n";
  }
  //advanced settings
  $qry="SELECT po.`name`,po.`account`,pto.`value`,pto.`mode` ";
  $qry.="FROM cw_prov_option AS po ";
  $qry.="JOIN cw_prov_template_option AS pto ON pto.`id_option`=po.`id` ";
  $qry.="WHERE pto.`id_template`='".$template["id"]."' ";
  $qry.="AND po.`account` NOT IN (5,6,7,8) ";
  $res=$db->Execute($qry);
  if(($res===false)||($template["id"]=="")) printDebug("ERROR (unable to retrieve options for template ".$template["id"].")",true);
  while($option=$res->FetchRow()){
  	if((in_array($option["account"],array("1","2","3","4"),true))||(in_array($option["account"],array(1,2,3,4),true))){
    	$flag=" idx=\"1\""; 
    }else $flag="";
    if(trim($option["mode"])=="") $option["mode"]="&";
  	echo "<".trim($option["name"]).$flag." perm=\"".trim($option["mode"])."\">".sanitizeXml(trim($option["value"]))."</".trim($option["name"]).">\n";
  }
  //firmware settings
  $f_log="";
  if($template["firmware"]!=0){
		echo "<firmware_status perm=\"&\">".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/firmware/index.php?mac=".$mac)."</firmware_status>\n";
    $f_log=" SW ";
  }
  echo "</phone-settings>\n";
  /****************************/
  /****  FIRMWARE OPTIONS  ****/
  /****************************/
  //echo "<firmware-settings>\n";
	//echo "<firmware perm=\"\"></firmware>\n";
	//echo "</firmware-settings>\n";
  /****************************/
  /*****  FUNCTION KEYS  ******/
  /****************************/
  echo "<functionKeys>\n";
	$qry="SELECT po.`name`,po.`account`,pto.`value`,pto.`mode` ";
  $qry.="FROM cw_prov_option AS po ";
  $qry.="JOIN cw_prov_template_option AS pto ON pto.`id_option`=po.`id` ";
  $qry.="WHERE pto.`id_template`='".$template["id"]."' ";
  $qry.="AND po.`account` IN (5,6,7,8) ";
  $res=$db->Execute($qry);
  if(($res===false)||($template["id"]=="")) printDebug("ERROR (unable to retrieve fucntion keys for template ".$template["id"].")",true);
  $fkeysArr=array();
  $fcontextArr=array();
  while($option=$res->FetchRow()){
  	if((in_array($option["account"],array("5","6"),true))||(in_array($option["account"],array(5,6),true))){
    	$idx=intval(trim(substr($option["name"],strlen("fkey"))));
  		$fkeysArr[$idx]=array(
          "value" => trim($option["value"]),
          "mode" => trim($option["mode"])
      	);
  	}else{
    	$idx=intval(trim(substr($option["name"],strlen("fkey_context"))));
    	$fcontextArr[$idx]=array(
          "value" => trim($option["value"]),
          "mode" => trim($option["mode"])
      	);
    }
	}
  if(is_array($fkeysArr)){
  	foreach($fkeysArr as $k=>$v){
    	if((is_array($fcontextArr[$k]))&&(isset($fcontextArr[$k]["value"]))&&(trim($fcontextArr[$k]["value"])!="")){
      	$context=trim($fcontextArr[$k]["value"]);
      }else $context="active";
    	echo "<fkey idx=\"".$k."\" context=\"".$context."\" perm=\"".$v["mode"]."\">".sanitizeXml($v["value"])."</fkey>\n";
    }
  }
  echo "</functionKeys>\n";
  /*************************/
  /***     DIRECTORY    ****/
  /*************************/
	echo "<tbook>\n";
  if($template["directory"]!=0){
  	$qry="SELECT dc.`type`,d.`phonenumber_prefix`,d.`mobile_prefix`,c.`cid_name`,c.`phonenumber`,c.`mobile` ";
    $qry.="FROM cw_prov_directory_contacts AS dc ";
    $qry.="JOIN cw_contacts AS c ON c.`id`=dc.`id_contact` ";
    $qry.="JOIN cw_prov_directory AS d ON d.`id`=dc.`id_directory` ";
    $qry.="WHERE dc.`id_directory`='".$template["directory"]."' ";
    $qry.="ORDER BY c.`order_field` ";
    $res=$db->Execute($qry);
    if(($res===false)||($template["directory"]=="")) printDebug("ERROR (unable to retrieve directory for template ".$template["id"].")",true);
    $d_num=0;
    while($contact=$res->FetchRow()){
    	$number="";
  		if(($contact["type"]==1)||($contact["type"]=="1")){
      	$number=trim($contact["phonenumber"]);
        if(trim($contact["phonenumber_prefix"])!="") $prefix=trim($contact["phonenumber_prefix"]); else $prefix="";
      }else if(($contact["type"]==2)||($contact["type"]=="2")){
      	$number=trim($contact["mobile"]);
        if(trim($contact["mobile_prefix"])!="") $prefix=trim($contact["mobile_prefix"]); else $prefix="";
      }
      if((trim($contact["cid_name"])!="")&&($number!="")){
      	echo "<item context=\"active\" type=\"none\" index=\"".$d_num."\">\n";
   			echo "<name>".sanitizeXml($prefix.trim($contact["cid_name"]))."</name>\n";
   			echo "<number>".sanitizeXml($number)."</number>\n";
  			echo "</item>\n";
				$d_num++;
      }
    }
  }
  echo "</tbook>\n";
  /*************************/
  /***     LANGUAGES    ****/
  /*************************/
  //gui
  echo "<gui-languages>\n";
  echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/gui_lang_CA.xml")."\" name=\"Catalan\"/>\n";
  echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/gui_lang_CZ.xml")."\" name=\"Cestina\"/>\n";
  echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/gui_lang_DK.xml")."\" name=\"Dansk\"/>\n";
  echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/gui_lang_DE.xml")."\" name=\"Deutsch\"/>\n";
  echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/gui_lang_EN.xml")."\" name=\"English\"/>\n";
  echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/gui_lang_UK.xml")."\" name=\"English(UK)\"/>\n";
  echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/gui_lang_SP.xml")."\" name=\"Espanol\"/>\n";
  echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/gui_lang_FR.xml")."\" name=\"Francais\"/>\n";
  echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/gui_lang_IT.xml")."\" name=\"Italiano\"/>\n";
  echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/gui_lang_NL.xml")."\" name=\"Nederlands\"/>\n";
  echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/gui_lang_NO.xml")."\" name=\"Norsk\"/>\n";
  echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/gui_lang_PL.xml")."\" name=\"Polski\"/>\n";
  echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/gui_lang_PT.xml")."\" name=\"Portugues\"/>\n";
  echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/gui_lang_RU.xml")."\" name=\"Russian\"/>\n";
  echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/gui_lang_SK.xml")."\" name=\"Slovencina\"/>\n";
  echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/gui_lang_FI.xml")."\" name=\"Suomi\"/>\n";
  echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/gui_lang_SW.xml")."\" name=\"Svenska\"/>\n";
  echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/gui_lang_TR.xml")."\" name=\"Turkce\"/>\n";
  echo "</gui-languages>\n";
  //web
  echo "<web-languages>\n";
 	echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/web_lang_CZ.xml")."\" name=\"Cestina\"/>\n";
 	echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/web_lang_DK.xml")."\" name=\"Dansk\"/>\n";
 	echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/web_lang_DE.xml")."\" name=\"Deutsch\"/>\n";
 	echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/web_lang_EN.xml")."\" name=\"English\"/>\n";
 	echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/web_lang_SP.xml")."\" name=\"Espanol\"/>\n";
 	echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/web_lang_FR.xml")."\" name=\"Francais\"/>\n";
 	echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/web_lang_IT.xml")."\" name=\"Italiano\"/>\n";
	echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/web_lang_NL.xml")."\" name=\"Nederlands\"/>\n";
 	echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/web_lang_NO.xml")."\" name=\"Norsk\"/>\n";
 	echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/web_lang_PT.xml")."\" name=\"Portugues\"/>\n";
 	echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/web_lang_RU.xml")."\" name=\"Russian\"/>\n";
 	echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/web_lang_FI.xml")."\" name=\"Suomi\"/>\n";
 	echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/web_lang_SW.xml")."\" name=\"Svenska\"/>\n";
 	echo "<language url=\"".sanitizeXml("http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/languages/web_lang_TR.xml")."\" name=\"Turkce\"/>\n";  
  echo "</web-languages>\n";
  //close configuration
  echo "</settings>";
?>